/**
 * 
 */
package org.neptune.common.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author dell 解压文件工具类
 */
public class UnZipFileUtil {

	private static final Logger logger = LoggerFactory.getLogger(UnZipFileUtil.class);

	public static final String TEMP_PATH = "/temp";

	public static final String ZIP_SUFFIX = ".zip";

	public static final String JAR_SUFFIX = ".jar";

	public static void uncompressZip(String sourceFilePath) throws IOException {
		logger.info("start unzip file :" + sourceFilePath);
		File sourceFile = new File(sourceFilePath);
		String fileName = null;
		if (sourceFile.isFile() && (sourceFile.getName().endsWith(ZIP_SUFFIX)
				|| sourceFile.getName().endsWith(JAR_SUFFIX))){
			logger.info("start####################");
			fileName = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("."));

			File tempFile = new File(fileName);
			if (tempFile.exists()){
				clearTargetFolder(fileName);
			}

			ZipFile zipFile = new ZipFile(sourceFilePath);
			Enumeration<ZipArchiveEntry> enumeration = zipFile.getEntries();
			ZipArchiveEntry zipArchiveEntry;
			while (enumeration.hasMoreElements()){
				zipArchiveEntry = enumeration.nextElement();
				File file = new File(fileName, zipArchiveEntry.getName());
				// 创建完整路径
				if (zipArchiveEntry.isDirectory()){
					file.mkdirs();
					continue;
				} else{
					file.getParentFile().mkdirs();
				}
				InputStream is = zipFile.getInputStream(zipArchiveEntry);
				OutputStream os = new FileOutputStream(file);
				IOUtils.copy(is, os, 4096);
				is.close();
				os.close();
			}
			zipFile.close();
		}
		logger.info("end unzip file :" + sourceFilePath);
	}

	/**
	 * 复制到目标文件夹
	 * 
	 * @param sourceFolder
	 * @param targetFolder
	 * @throws IOException
	 */
	public static void copyToTargetFolder(String sourceFolder, String targetFolder)
			throws IOException {
		File sourceFile = new File(sourceFolder);
		File targetFile = new File(targetFolder);

		if (!sourceFile.exists() || !sourceFile.isDirectory()){
			logger.info("source folder doesn't exist:" + sourceFile);
			return;
		}
		if (targetFile.exists()){
			targetFile.mkdirs();
		}

		FileUtils.copyDirectory(sourceFile, targetFile);

	}

	/**
	 * 清空目标文件夹
	 * 
	 * @param targetFolder
	 * @throws IOException
	 */
	public static void clearTargetFolder(String targetFolder) throws IOException {
		FileUtils.deleteDirectory(new File(targetFolder));
	}

	/**
	 * 新建文件夹
	 * 
	 * @param path
	 */
	public static void createFolder(String path) {
		File file = new File(path);
		if (file.exists()){
			file.delete();
		}
		file.mkdirs();
	}
}
